package com.weihe.toolkit.object;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class StringToolkit {
	/**
	 * 
	 * @param str - hello {0}
	 * @param values - "hoojo"
	 * @return - hello hoojo
	 */
	public static String repalceText(String str,Object[] values){
		if(StringUtils.isNotBlank(str)&& null!=values && values.length>0){
			for(int i=0;i<values.length;i++){
				str = str.replace("\\{"+i+"\\}", values[i].toString());
			}
		}
		return str;
	}
	
	/**
	 * 
	 * @param str - hello {name}
	 * @param json - {'name':"hoojo"}
	 * @return - hello hoojo
	 */
	@SuppressWarnings("unchecked")
	public static String repalceTextByJson(String str,String json){
		if(StringUtils.isNotBlank(str)){
			ObjectMapper objectMapper = new ObjectMapper();
			Map<String, Object> map;
			try {
				objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
				map = objectMapper.readValue(json, Map.class);
				Iterator<Entry<String, Object>> iterator = map.entrySet().iterator();
				while (iterator.hasNext()) {
					Entry<String, Object> entry= (Entry<String, Object>) iterator.next();
					str = str.replace("{"+entry.getKey()+"}", entry.getValue().toString());
			    }
			} catch (JsonParseException e) {
				e.printStackTrace();
			} catch (JsonMappingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return str;
	}
	
	public static void main(String[] args) throws ParseException, IOException{
//		int acount = 20;
//		for(int i=0;i<20;i++){
//			List<String> list = FileToolkit.readStringByLines("D:\\social.sql",i*100000/acount+1,(i+1)*100000/acount);
//			FileToolkit.writeToFile("D:\\"+i+".sql", list);
//		}
		String str = "2014-5-30";
//		Long s = 1401418320479L;
		Long s = 1401416127649L;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		System.out.println(sdf.format(new Date(s)));
		
		int jcount = 100000/30;
		long ss = 84000/jcount;
		long cc = 25L*3600*24*1000;
		System.out.println(84000/jcount);
		System.out.println(ss);
		System.out.println(cc);
	}
}
